package cn.icanci.ladybird.controller;

import cn.icanci.ladybird.common.result.JsonData;
import cn.icanci.ladybird.common.util.cache.RedisUtils;
import cn.icanci.ladybird.pojo.dto.UserProduceDTO;
import cn.icanci.ladybird.service.TicketDetailsService;
import cn.icanci.ladybird.service.UserInfoService;
import cn.icanci.ladybird.service.user.UserProduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.TimeUnit;

/**
 * @author: zxp
 * @date: Created in 2020/10/2 0002 下午 21:06
 * @classAction: 商品数据展示使用的Controller
 */

@SuppressWarnings("all")
@Controller
@RequestMapping("/api/v1/pub/user")
public class UserProduceController {
    @Autowired
    private UserProduceService userProduceService;
    @Autowired
    private TicketDetailsService ticketDetailsService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RedisUtils redisUtils;

    @ResponseBody
    @RequestMapping("/userproduce")
    public JsonData getUserIndex(Long ticketId) {
        UserProduceDTO userProduceDTO = userProduceService.queryUserProduceDTO(ticketId);
        return JsonData.builderSuccess(userProduceDTO);
    }


    @RequestMapping("/json/userproduce")
    public String getUserIndexJson(Model model, Long ticketId) {
        String key = "TicketData-" + ticketId;
        JsonData targetData = (JsonData) redisUtils.get(key);
        if (null == targetData) {
            UserProduceDTO userProduceDTO = userProduceService.queryUserProduceDTO(ticketId);
            JsonData jsonData = JsonData.builderSuccess(userProduceDTO);
            model.addAttribute("jsonData", jsonData);
            redisUtils.set(key, jsonData, 30, TimeUnit.MINUTES);
        } else {
            model.addAttribute("jsonData", targetData);
        }
        return "user/detalis";
    }

}
